//
//  RegiestController.m
//  YiBaoStore
//
//  Created by 毛云河 on 2018/10/1.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "RegiestController.h"

@interface RegiestController ()<UITextFieldDelegate>

@property (nonatomic, strong) NSDictionary *style;
@property (nonatomic,strong) UITextField *phoneTF;
@property (nonatomic,strong) UITextField *codeTF;
@property (nonatomic,strong) UIButton *codeBtn;
@property (nonatomic,strong) UIButton *nextBtn;
@end

@implementation RegiestController

- (NSDictionary *)style
{
    if (!_style) {
        _style = [NSDictionary dictionary];
        _style = @{
                   @"gray20":@[UIColorFromHex(0x343434),[UIFont fontWithName:@"DINAlternate-Bold" size:20]],
                   @"gray12":@[UIColorFromHex(0x343434),[UIFont fontWithName:@"PingFangSC-Semibold" size:12]],
                   @"black12":@[UIColorFromHex(0x343434),FontWithSize(12)]
                   };
    }
    return _style;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.bgNavView.hidden = YES;
    self.view.backgroundColor = TotalWhite;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavi];
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TextFieldChange) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)setNavi
{
    UIView *navi = [UIView new];
    if (@available(iOS 11.0, *)) {
        navi.frame = CGRectMake(0, TopStatusHeight_xs, SCREENWIDTH, 48);
    } else {
        // Fallback on earlier versions
        navi.frame = CGRectMake(0, TopStatusHeight, SCREENWIDTH, 48);
    }
    navi.backgroundColor = [UIColor clearColor];
    [self.view addSubview:navi];
    
    UIImageView *leftIamge = [UIImageView new];
    leftIamge.frame = CGRectMake(10, 32, 18, 14);
    leftIamge.image = [UIImage imageNamed:@"back"];
    [navi addSubview:leftIamge];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 10, 40, 40);
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [navi addSubview:leftBtn];
    
    UILabel *titleLab = [UILabel new];
    titleLab.frame = CGRectMake(0, 30, SCREENWIDTH, 18);
    titleLab.text = @"注册";
    titleLab.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:18];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = UIColorFromHex(0x343434);
    [navi addSubview:titleLab];
    
    UILabel *headLab = [UILabel new];
    headLab.frame = CGRectMake(2*leftPadding, navi.bottom+50, 120, 65);
    headLab.textColor = UIColorFromHex(0x343434);
    headLab.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:22];
    headLab.numberOfLines = 2;
    headLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:3 and:@"新用户\n登录流程"];
    [self.view addSubview:headLab];
    
    UILabel *rightLab = [UILabel new];
    rightLab.frame = CGRectMake(SCREENWIDTH-70, headLab.top+20, 30, 50);
    rightLab.numberOfLines = 2;
    rightLab.attributedText = [[NSString stringWithFormat:@"<gray20>1</gray20><gray12>%@</gray12><black12>%@</black12>",@"/ 2\n",@"步 骤"]attributedStringWithStyleBook:self.style];
    [self.view addSubview:rightLab];
    
    UIImageView *blindIV = [UIImageView new];
    blindIV.frame = CGRectMake(2*leftPadding, headLab.bottom+40, 60, 60);
    blindIV.image = [UIImage imageNamed:@"blind"];
    [self.view addSubview:blindIV];
    
    UILabel *blindLab = [UILabel new];
    blindLab.frame = CGRectMake(blindIV.right+16, blindIV.top+20, 60, 20);
    blindLab.textColor = UIColorFromHex(0x343434);
    blindLab.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:14];
    blindLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:@"手机绑定"];
    [self.view addSubview:blindLab];
    
    UILabel *phoneLab = [UILabel new];
    phoneLab.frame = CGRectMake(2*leftPadding, blindIV.bottom+59, 55, 12);
    phoneLab.textColor = goldColor;
    phoneLab.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
    phoneLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:@"手机号码"];
    [self.view addSubview:phoneLab];
    
    self.phoneTF = [UITextField new];
    self.phoneTF.frame = CGRectMake(phoneLab.left, phoneLab.bottom+10, SCREENWIDTH-80, 16);
    self.phoneTF.delegate = self;
    //self.phoneTF.secureTextEntry = YES;
    self.phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneTF.returnKeyType = UIReturnKeyNext;
    self.phoneTF.textColor = titleColor;
    UIButton *button = [self.phoneTF valueForKey:@"_clearButton"];
    [button setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    self.phoneTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.phoneTF.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    self.phoneTF.placeholder = @"请输入手机号码";
    [self.phoneTF setValue:UIColorFromHex(0xD8D8D8) forKeyPath:@"_placeholderLabel.textColor"];
    //[self.phoneTF setValue:[UIColor colorWithRed:208/255.0 green:208/255.0 blue:210/255.0 alpha:0.3] forKeyPath:@"_placeholderLabel.textColor"];
    [self.phoneTF setValue:FontWithSize(16) forKeyPath:@"_placeholderLabel.font"];
    [self.view addSubview:self.phoneTF];
    
    UIView *lineView = [UIView new];
    lineView.frame = CGRectMake(phoneLab.left, _phoneTF.bottom+10, SCREENWIDTH-80, 1);
    lineView.backgroundColor = BacklineColor;
    [self.view addSubview:lineView];
    
    UILabel *codeLab = [UILabel new];
    codeLab.frame = CGRectMake(2*leftPadding, lineView.bottom+20, 55, 12);
    codeLab.textColor = goldColor;
    codeLab.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
    codeLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:@"验证码"];
    [self.view addSubview:codeLab];
    
    self.codeTF = [UITextField new];
    self.codeTF.frame = CGRectMake(phoneLab.left, codeLab.bottom+10, (SCREENWIDTH-80)*0.6, 16);
    self.codeTF.delegate = self;
    self.codeTF.keyboardType = UIKeyboardTypeNumberPad;
    self.codeTF.returnKeyType = UIReturnKeyDone;
    self.codeTF.textColor = titleColor;
    self.codeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.codeTF.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];;
    self.codeTF.placeholder = @"请输入手机验证码";
    [self.codeTF setValue:UIColorFromHex(0xD8D8D8) forKeyPath:@"_placeholderLabel.textColor"];
    [self.codeTF setValue:FontWithSize(16) forKeyPath:@"_placeholderLabel.font"];
    [self.view addSubview:self.codeTF];
    
    UIView *mid = [UIView new];
    mid.frame = CGRectMake(self.codeTF.right, self.codeTF.top+3, 1, 10);
    mid.backgroundColor = BacklineColor;
    [self.view addSubview:mid];
    
    self.codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat wid = (SCREENWIDTH-80)*0.4;
    self.codeBtn.frame = CGRectMake(mid.right+15, self.codeTF.top, wid-16, 16);
    self.codeBtn.titleLabel.font = FontWithSize(16);
    [self.codeBtn setAttributedTitle:[TTKUtil getAtttributeStringColorWithLineSpace:0 kern:1.33 and:@"发送验证码"] forState:UIControlStateNormal];
    [self.codeBtn setTitleColor:UIColorFromHex(0x8D8D8D) forState:UIControlStateNormal];
    [self.codeBtn addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.codeBtn];
    
    UIView *MidlineView = [UIView new];
    MidlineView.frame = CGRectMake(phoneLab.left, self.codeTF.bottom+10, SCREENWIDTH-80, 1);
    MidlineView.backgroundColor = BacklineColor;
    [self.view addSubview:MidlineView];
    
    self.nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextBtn.frame = CGRectMake(2*leftPadding, MidlineView.bottom+118, SCREENWIDTH-80, 50);
    self.nextBtn.layer.cornerRadius = 25;
    self.nextBtn.layer.masksToBounds = YES;
    [self.nextBtn setAttributedTitle:[TTKUtil getAtttributeStringColorWithLineSpace:0 kern:1.5 and:@"下一步"] forState:UIControlStateNormal];
    self.nextBtn.backgroundColor = UIColorFromHex(0xD1D1D1);
    self.nextBtn.titleLabel.font = FontWithSize(18);
    [self.nextBtn setTitleColor:UIColorFromHex(0x8D8D8D) forState:UIControlStateNormal];
    [self.nextBtn addTarget:self action:@selector(nextBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.nextBtn];
}

- (void)TextFieldChange
{
    NSString *phone = self.phoneTF.text;
    NSString *code = self.codeTF.text;
    if (self.phoneTF.text.length > 11) {
        phone = [self.phoneTF.text substringToIndex:11];
    }
    if (self.codeTF.text.length > 6) {
        code = [self.codeTF.text substringToIndex:6];
    }
    if (phone.length>0) {
        self.phoneTF.attributedText = [TTKUtil getAtttributeStringColorWithLineSpace:0 kern:1.33 and:phone];
        self.phoneTF.textColor = titleColor;
    }
    if (code.length>0) {
        self.codeTF.attributedText = [TTKUtil getAtttributeStringColorWithLineSpace:0 kern:1.33 and:code];
        self.codeTF.textColor = titleColor;
    }
    if (phone.length == 11 && code.length == 6) {
        self.nextBtn.userInteractionEnabled = YES;
        self.nextBtn.backgroundColor = goldColor;
        self.nextBtn.titleLabel.textColor = titleColor;
    }else
    {
        self.nextBtn.userInteractionEnabled = NO;
        self.nextBtn.backgroundColor = UIColorFromHex(0xD1D1D1);
        self.nextBtn.titleLabel.textColor = UIColorFromHex(0x8D8D8D);
    }
}

#pragma mark -- 获取验证码
- (void)getCode:(UIButton *)btn
{
    NSString *phone = self.phoneTF.text;
    if([phone isEqualToString:@""])
    {
        [TTKUtil hudShowMessage:@"请输入手机号码"];
        return;
    }
    if (![TTKStringUtils isValidateMobile:phone])
    {
        [TTKUtil hudShowMessage:@"请输入正确的手机号码"];
        return;
    }
    [self startCodeBtnTimeWithBtn:btn];
}

#pragma mark -- 获取验证码
- (void)startCodeBtnTimeWithBtn:(UIButton *)btn
{
    __block int timeout = 59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        NSString *title = @"重新发送";
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [btn setAttributedTitle:[TTKUtil getAtttributeStringColorWithLineSpace:0 kern:1.33 and:title] forState:UIControlStateNormal];
                btn.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout %60 ;
            NSString *name = @"重新发送";
            NSString *strTime = [NSString stringWithFormat:@"%@%d",name,seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //NSLog(@"____%@",strTime);
                // [btn setTitle:[NSString stringWithFormat:@"%@",strTime] forState:UIControlStateNormal];
                [btn setAttributedTitle:[TTKUtil getAtttributeStringColorWithLineSpace:0 kern:1.33 and:strTime] forState:UIControlStateNormal];
                btn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    
    dispatch_resume(_timer);
}

- (void)nextBtn:(UIButton *)btn
{
    [self.navigationController pushViewController:[ADDNewShopViewController new] animated:YES];
}

- (void)leftBtnClick:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
